set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
#set(CMAKE_SYSTEM_PROCESSOR arm)
cmake_minimum_required(VERSION 3.26)

# specify cross-compilers and tools
set(CMAKE_C_COMPILER armclang)
set(CMAKE_C_COMPILER_WORKS TRUE)
set(CMAKE_OBJCOPY fromelf)
#SET(CMAKE_C_COMPILER_WORKS TRUE)
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")

set(CMAKE_C_LINK_EXECUTABLE armlink.exe)
set(CMAKE_ASM_LINK_EXECUTABLE armlink.exe)

#set(CMAKE_CXX_COMPILER_LAUNCHER ccache)
#set(CMAKE_C_COMPILER_LAUNCHER ccache)

# project settings
project(at32f413_template C ASM)
set(CMAKE_C_STANDARD 11)

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/project/misc/AT32F413xB.sct)

add_compile_definitions(-DAT_START_F413_V1
        -DAT32F413CBT7
        -DUSE_STDPERIPH_DRIVER)

include_directories(
        "project/inc"
        "project/board"
        "libraries/cmsis/cm4/core_support"
        "libraries/cmsis/cm4/device_support"
        "libraries/drivers/inc")

file(GLOB_RECURSE SOURCES
        "project/src/*.c"
        "project/board/*.c"
        "libraries/cmsis/cm4/device_support/system_at32f413.c"
        "libraries/drivers/src/*.c"
        "project/src/startup_at32f413.s")

set(CPU -mcpu=cortex-m4)
set(FPU -mfpu=fpv4-sp-d16 -mfloat-abi=hard)

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    message(STATUS "Maximum optimization for speed")
    set(OPTIMIZATION_FLAGS -g -O3)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
    message(STATUS "Maximum optimization for speed, debug info included")
    set(OPTIMIZATION_FLAGS -Ofast -g)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
    message(STATUS "Maximum optimization for size")
    set(OPTIMIZATION_FLAGS -Os)
else ()
    message(STATUS "Minimal optimization, debug info included")
    set(OPTIMIZATION_FLAGS -O1 -g)
endif ()
add_compile_options(--target=arm-arm-none-eabi)

set(c_flags -xc -std=c99 ${CPU} ${FPU} -c -fno-rtti -funsigned-char -fshort-enums -fshort-wchar
        -gdwarf-4 ${OPTIMIZATION_FLAGS} -ffunction-sections -fdata-sections
        -Wno-packed
        -Wno-missing-variable-declarations
        -Wno-missing-prototypes
        -Wno-missing-noreturn
        -Wno-sign-conversion
        -Wno-nonportable-include-path
        -Wno-reserved-id-macro
        -Wno-unused-macros
        -Wno-documentation-unknown-command
        -Wno-documentation
        -Wno-license-management
        -Wno-parentheses-equality
        -Wno-reserved-identifier
        -MD)
set(asm_flags ${CPU} ${FPU} -masm=auto -Wa,armasm,--diag_suppress=A1950W -c
        -gdwarf-4)

add_compile_options(
        "$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
        "$<$<COMPILE_LANGUAGE:ASM>:${asm_flags}>"
)

add_link_options(--cpu=Cortex-M4.fp.sp)
add_link_options(--strict --scatter=${LINKER_SCRIPT})
add_link_options(--summary_stderr)
add_link_options(--info=summarysizes)
add_link_options(--map --xref --callgraph --symbols --info=sizes --info=totals --info=unused --info=veneers)


add_executable(${PROJECT_NAME} ${SOURCES})

set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} --i32 $<TARGET_FILE:${PROJECT_NAME}> --output=${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} --bin $<TARGET_FILE:${PROJECT_NAME}> --output=${BIN_FILE}
        COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")

add_custom_command(OUTPUT upload
        COMMENT "Upload $<TARGET_FILE:${PROJECT_NAME}>"
        COMMAND pyocd load $<TARGET_FILE:${PROJECT_NAME}> --config ../pyocd.yaml
        USES_TERMINAL
)

add_custom_target(swd_upload
        DEPENDS ${PROJECT_NAME} upload
)

add_custom_target(swd_erase
        COMMENT "Erase Chip"
        COMMAND pyocd erase --chip --config ../pyocd.yaml
        USES_TERMINAL
        VERBATIM
)
